package com.cwvs.controller.admin;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cwvs.services.AppAreaService;
import com.cwvs.services.AppStoreService;
import com.cwvs.services.AppUserService;
import com.cwvs.utils.Pages;

/**
 * 管理员登录
 * 
 * @author Administrator
 * 
 */
@Controller
@RequestMapping("/admin")
public class AdminUserLogin {
	@Resource
	private AppUserService appUserService;
	@Resource
	private AppStoreService appStoreService;
	@Resource
	private AppAreaService appAreaService;

	/**
	 * 首页
	 * 
	 * @return
	 */
	@RequestMapping("/task_url")
	public ModelAndView task(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("task");
		return mv;
	}

	@RequestMapping("/area_view")
	public ModelAndView areaView(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("app_area/app_area_list");
		return mv;
	}

	@RequestMapping("/area_list")
	public @ResponseBody
	List<Map<String, Object>> areaList(HttpServletRequest request,
			HttpServletResponse response) {
		List<Map<String, Object>> ls = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> data = appAreaService.areaList(request
				.getParameter("node"));
		for (Map<String, Object> m : data) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("id", MapUtils.getString(m, "ID"));
			map.put("text", MapUtils.getString(m, "NAME"));
			map.put("level", MapUtils.getString(m, "LEVEL"));
			map.put("leaf",
					StringUtils.equalsIgnoreCase("4",
							MapUtils.getString(m, "LEVEL")));
			ls.add(map);
		}
		return ls;
	}

	@RequestMapping("/area_edit")
	public ModelAndView areaEdit(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("app_area/app_area_edit");
		String areaId = request.getParameter("area_id");
		mv.addObject("parent", appAreaService.getParentArea(areaId));
		return mv;
	}

	@SuppressWarnings("rawtypes")
	@RequestMapping("/area_save")
	public @ResponseBody
	Map<String, Object> areaSave(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String,Object> map=new HashMap<String,Object>();
		Enumeration paramNames = request.getParameterNames();
		while (paramNames.hasMoreElements()) {
			String paramName = (String) paramNames.nextElement();
			String[] paramValues = request.getParameterValues(paramName);
			if (paramValues.length == 1) {
				String paramValue = paramValues[0];
				if (paramValue.length() != 0) {
					map.put(paramName.toUpperCase(), paramValue);
				}
			}
		}
		Map<String,Object> res=new HashMap<String,Object>();
		boolean b=appAreaService.saveOrUpdate(map);
		if(b){
			res.put("message", "、处理成功");
		}else{
			res.put("errormsg", "处理失败");
		}
		return res;
	}
	
	@RequestMapping("/user_view")
	public ModelAndView userView(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("app_user/app_users_list");
		return mv;
	}
	
	@RequestMapping("/user_list")
	public @ResponseBody
	Map<String, Object>  userList(HttpServletRequest request,
			HttpServletResponse response) {
		Pages page = new Pages();
		String limit = request.getParameter("limit");
		String start = request.getParameter("start");
		page.setPageSize(Integer.parseInt(limit));
		page.setCurrentPage(Integer.parseInt(start) / Integer.parseInt(limit)
				+ 1);
		HashMap<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("USERNAME", request.getParameter("name"));
		paramsMap.put("CTIME", request.getParameter("ctime"));
		paramsMap.put("ETIME", request.getParameter("etime"));
		Pages rePage = appUserService.userList(paramsMap, page);
		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("rows", rePage.getPage());
		ret.put("totalCount", rePage.getTotalRecord());
		return ret;
	}
	/**
	 * 更新用户状态
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/user_tf")
	public @ResponseBody
	Map<String, Object> userTf(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> res = new HashMap<String, Object>();
		String id = request.getParameter("id");
		String isActive = request.getParameter("isActive");
		boolean status=StringUtils.equalsIgnoreCase(isActive, "0");
		if(appUserService.updateUserStatus(id, status)){
			res.put("message", "更新成功");
			res.put("success", true);
			return res;
		}
		res.put("errormsg", "更新失败");
		res.put("success", false);
		return res;

	}
}
